#!/bin/python3

import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk

from core import *



class Main(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title='Temperature')
        self.set_size_request(180, 60)
        self.set_border_width(6)

        vbox = Gtk.Box()
        self.add(vbox)

        ad = Gtk.Adjustment(value=0, lower=-100000, upper=100000, step_increment=0.5)
        self.start_t = Gtk.SpinButton(adjustment=ad, climb_rate=3, digits=3)
        self.start_t.connect("changed", self.on_chenges)
        vbox.add(self.start_t)

        types_store = Gtk.ListStore(str)
        self.types = ('C', 'K', 'F')
        for i in self.types:
            types_store.append([i])
        renderer_text = Gtk.CellRendererText()

        self.start_type = Gtk.ComboBox.new_with_model(types_store)
        self.start_type.pack_start(renderer_text, True)
        self.start_type.add_attribute(renderer_text, "text", 0)
        self.start_type.set_size_request(60, 40)
        self.start_type.set_active(0)
        self.start_type.connect("changed", self.on_chenges)
        vbox.add(self.start_type)

        self.finish_type = Gtk.ComboBox.new_with_model(types_store)
        self.finish_type.pack_start(renderer_text, True)
        self.finish_type.add_attribute(renderer_text, "text", 0)
        self.finish_type.set_size_request(60, 40)
        self.finish_type.set_active(1)
        self.finish_type.connect("changed", self.on_chenges)
        vbox.add(self.finish_type)

        self.finish_t = Gtk.Label()
        self.finish_t.set_size_request(100, 40)
        vbox.add(self.finish_t)

    def on_chenges(self, widget):
        start_t = self.start_t.get_value()
        start_type = self.types[self.start_type.get_active()]
        finish_type = self.types[self.finish_type.get_active()]
        finish_t = round(get_func[f"{start_type}>{finish_type}"](start_t), 2)
        self.finish_t.set_text(str(finish_t))



def run_gtk_gui():
    win = Main()
    win.set_resizable(False)
    win.connect('destroy', Gtk.main_quit)
    win.show_all()
    Gtk.main()

if __name__ == "__main__":
    run_gtk_gui()
